home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Extension Shell 1.5 / Sample Extensions (1.5) / Shutdown Fade ƒ / Gamma.c < prev    next >
Text File  |  1996-04-12  |  6KB  |  194 lines

  1. /*    NAME:
  2.         Gamma.c
  3.  
  4.     WRITTEN BY:
  5.         Matt Slot
  6.  
  7.     MODIFIED BY:
  8.         Dair Grant
  9.                                 
  10.     DESCRIPTION:
  11.         Cut down version of Matt Slot's Gamma Utils Library source code. The
  12.         routines to deal with a given graphics device have been removed, and
  13.         the only calls now supported are those that work on all graphics
  14.         devices at once.
  15.  
  16.     ___________________________________________________________________________
  17. */
  18. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  19. //   Last updated 3/13/93, MJS
  20. //
  21. // * ****************************************************************************** *
  22. //    Stripped down version for Shutdown Fade
  23. // * ****************************************************************************** *
  24. //
  25. //
  26. // * ****************************************************************************** *
  27. //
  28. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  29. //        new functionality into the library or make an A4-based library. 
  30. //    See the header file "gamma.h" for much more information. -- MJS
  31. //
  32. // * ****************************************************************************** *
  33. #include <OSUtils.h>
  34. #include <Devices.h>
  35. #include <Memory.h>
  36. #include <GestaltEqu.h>
  37. #include <Quickdraw.h>
  38. #include <Video.h>
  39. #include <Traps.h>
  40. #include "gamma.h"
  41.  
  42.  
  43. globalGammasHdl    gammaTables=0;
  44.  
  45.  
  46. // * ****************************************************************************** *
  47. // * ****************************************************************************** *
  48.  
  49. Boolean IsGammaAvailable() {
  50.     GDHandle theGDevice;
  51.  
  52.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  53.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  54.     
  55.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice))
  56.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  57.                 TestDeviceAttribute(theGDevice, noDriver)) return(0);
  58.  
  59.     return(-1);
  60.     }
  61.  
  62.  
  63. // * ****************************************************************************** *
  64. // * ****************************************************************************** *
  65.  
  66. OSErr SetupGammaTools() {
  67.     short errorCold=0;
  68.     globalGammasHdl tempHdl;
  69.     GammaTblPtr    masterGTable;
  70.     GDHandle theGDevice;
  71.  
  72.     
  73.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  74.         if (errorCold = GetDevGammaTable(theGDevice, &masterGTable)) return(errorCold);
  75.         
  76.         tempHdl = (globalGammasHdl) NewHandle(sizeof(globalGammas));
  77.         if (tempHdl == 0) return(errorCold = MemError());
  78.         
  79.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  80.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  81.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  82.         (*tempHdl)->theGDevice = theGDevice;
  83.         
  84.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  85.         if ((*tempHdl)->saved == 0) return(errorCold = MemError());
  86.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  87.         if ((*tempHdl)->hacked == 0) return(errorCold = MemError());
  88.     
  89.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  90.         
  91.         (*tempHdl)->next = gammaTables;
  92.         gammaTables = tempHdl;
  93.         }
  94.  
  95.     return(0);
  96.     }
  97.  
  98. // * ****************************************************************************** *
  99. // * ****************************************************************************** *
  100.  
  101. OSErr DoGammaFade(short percent) {
  102.     short errorCold=0;
  103.     register long size, i, theNum;
  104.     globalGammasHdl tempHdl;
  105.     unsigned char *dataPtr;
  106.  
  107.  
  108.  
  109.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  110.     
  111.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  112.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  113.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  114.         
  115.         for(i=0; i < size; i++) {
  116.             theNum = dataPtr[i];
  117.             theNum = (theNum * percent) / 100;
  118.             dataPtr[i] = theNum;
  119.             }
  120.         
  121.         if (errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  122.             return(errorCold);
  123.         }
  124.         
  125.     return(0);
  126.     }
  127.  
  128.  
  129. // * ****************************************************************************** *
  130. // * ****************************************************************************** *
  131.  
  132. OSErr DisposeGammaTools() {
  133.     globalGammasHdl tempHdl, nextHdl;
  134.  
  135.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  136.         nextHdl = (*tempHdl)->next;
  137.         DisposeHandle((Handle) (*tempHdl)->saved);
  138.         DisposeHandle((Handle) (*tempHdl)->hacked);
  139.         DisposeHandle((Handle) tempHdl);
  140.         }
  141.         
  142.     return(0);
  143.     }
  144.  
  145. // * ****************************************************************************** *
  146. // * ****************************************************************************** *
  147.  
  148. OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  149.     short errorCold=0;
  150.     CntrlParam  *myCPB;
  151.  
  152.     ((long *) theTable)[0] = 0;
  153.  
  154.             
  155.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  156.     myCPB->csCode = cscGetGamma;
  157.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  158.     *(GammaTblPtr **) myCPB->csParam = theTable;
  159.     errorCold = PBStatus((ParmBlkPtr) myCPB, 0);
  160.  
  161.     DisposePtr((Ptr) myCPB);
  162.     return(errorCold);
  163.     }
  164.  
  165. // * ****************************************************************************** *
  166. // * ****************************************************************************** *
  167.  
  168. OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  169.     CntrlParam *myCPB;
  170.     short errorCold=0;
  171.     CTabHandle cTab;
  172.     GDHandle saveGDevice;
  173.  
  174.  
  175.  
  176.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  177.     myCPB->csCode = cscSetGamma;
  178.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  179.     **(GammaTblPtr **) myCPB->csParam = *theTable;
  180.  
  181.     errorCold = PBControl((ParmBlkPtr) myCPB, 0);
  182.  
  183.     if (errorCold == 0) {
  184.         saveGDevice = GetGDevice();
  185.         SetGDevice(theGDevice);
  186.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  187.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  188.         SetGDevice(saveGDevice);
  189.         }
  190.  
  191.     DisposePtr((Ptr) myCPB);
  192.     return (errorCold);
  193.     }
  194.